package com.hmall.trade.listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PayStatusListener {
private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "hmall.pay.success.queue", durable = "true"),
            exchange = @Exchange(value = "hmall.pay.exchange", type = "direct", durable = "true"),
            key = "pay.success"
    ))
    public void handlePayStatus(Long orderId){
        //先做幂等性的校验，再去改变订单的状态
        // 1. 查询订单
        Order order = orderService.getById(orderId);
        // 2. 判断订单状态
        if (order == null ||order.getStatus() != 1) {
            return;
        }
        // 3. 修改订单状态
        orderService.markOrderPaySuccess(orderId);
//        System.out.println("收到支付结果，订单号：" + orderId);
    }
}
